package com.wuneng.ircontrol.module.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wuneng.ircontrol.R;
import com.wuneng.ircontrol.common.util.BaseFragment;
import com.wuneng.ircontrol.common.util.CariGson;
import com.wuneng.ircontrol.module.act.BleListAct;
import com.wuneng.ircontrol.module.act.MainActivity;
import com.wuneng.ircontrol.myUtil.LocaleHelper;
import com.wuneng.ircontrol.myUtil.common.LogUtil;
import com.wuneng.ircontrol.myUtil.common.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class FragmentA extends BaseFragment {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.btnRight)
    Button btnRight;
    @BindView(R.id.rlTop)
    RelativeLayout rlTop;
    Unbinder unbinder;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvOpen)
    TextView tvOpen;
    @BindView(R.id.tvFault)
    TextView tvFault;
    @BindView(R.id.tvTemp)
    TextView tvTemp;
    @BindView(R.id.tvHumidity)
    TextView tvHumidity;
    @BindView(R.id.btnOn)
    Button btnOn;
    @BindView(R.id.btnOff)
    Button btnOff;
    @BindView(R.id.btnRead)
    Button btnRead;
    public byte[] Open = {(byte) 0x01, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x88, (byte) 0x21};//阀门开
    public byte[] Close = {(byte) 0x01, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x89, (byte) 0xCA};//阀门关

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_a, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {
        rlBack.setVisibility(View.GONE);
        tvTitle.setText(R.string.title);
        tvRight.setText("搜索");
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivityForResult(new Intent(getActivity(), BleListAct.class), 1);
            }
        });
    }


    public void setDeviceName(String name) {
        tvName.setText(name);
    }

    public void clearData() {
        tvTemp.setText("- -");
        tvHumidity.setText("- -");
        tvOpen.setText("- -");
        tvFault.setText("- -");
    }

    @OnClick({R.id.btnOn, R.id.btnOff, R.id.btnRead})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnOn:

                String lang = LocaleHelper.getPersistedData(getActivity(), Locale.getDefault().getLanguage());
                Log.e("aaa", lang);
                if (lang.equals("en"))
                    LocaleHelper.setLocale(getActivity(), "zh");
                else
                    LocaleHelper.setLocale(getActivity(), "en");
                getActivity().recreate(); // 重新创建Activity以应用新的语言设置
                ((MainActivity) getActivity()).sendOrder(Open);
                break;
            case R.id.btnOff:
                ((MainActivity) getActivity()).sendOrder(Close);
                break;
            case R.id.btnRead:
                ((MainActivity) getActivity()).read();
                break;
        }
    }


    public void refresh(byte[] data) {
        LogUtil.write("收到查询返回数据" + Util.printData(data));
        int count = data[2] & 0xFF;
        switch (count) {
            case 4:
                List<Integer> results = new ArrayList<>();
                for (int i = 0; i < count / 2; i++) {
                    results.add((data[3 + 2 * i] & 0xFF) * 256 + data[4 + 2 * i] & 0xFF);
                }
                if (results.size() > 1) {
                    tvTemp.setText(results.get(0) + "℃");
                    tvHumidity.setText(results.get(1) + "%");
                }
                break;

            case 6:
                StringBuffer buffer = new StringBuffer();
                int a = (data[3] & 0xFF) * 256 + data[4] & 0xFF;
                tvOpen.setText(a + "%");
                for (int pos = 5; pos <= 8; pos++) {
                    final int s = pos;
                    byte[] res = byteToBitOfArray(data[pos]);
                    Log.e("aaa", CariGson.getInstance().toJson(res));
                    for (int i = 0; i < res.length; i++) {
                        if (res[i] == 1) {
                            buffer.append(getFault(8 * (s - 5) + i));
                        }
                    }
                }
                tvFault.setText(buffer.toString());
                break;

        }
    }


    public static byte[] byteToBitOfArray(byte b) {
        byte[] array = new byte[8];
        for (int i = 7; i >= 0; i--) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }


    private String getFault(int type) {
        String fault = "";
        switch (type) {
            case 2:
                fault = "电开过";
                break;

            case 3:
                fault = "电关过";
                break;

            case 4:
                fault = "机开过";
                break;

            case 5:
                fault = "机关过";
                break;

            case 6:
                fault = "堵转";
                break;

            case 7:
                fault = "未标校";
                break;


            case 8:
                fault = "编码器";
                break;

            case 15:
                fault = "综保错";
                break;

            case 16:
                fault = "A过压";
                break;

            case 17:
                fault = "A欠压";
                break;

            case 18:
                fault = "C过压";
                break;

            case 19:
                fault = "C欠压";
                break;

            case 20:
                fault = "A缺相";
                break;

            case 21:
                fault = "C缺相";
                break;

            case 22:
                fault = "A过流";
                break;

            case 23:
                fault = "B过流";
                break;
            case 24:
                fault = "C过流";
                break;
            case 25:
                fault = "不平衡";
                break;
            case 26:
                fault = "漏电";
                break;

        }


        return fault;
    }

}
